package br.usp.icmc.biomac.client;

import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import br.usp.icmc.biomac.client.mvp.AppActivityMapper;
import br.usp.icmc.biomac.client.mvp.AppPlaceHistoryMapper;
import br.usp.icmc.biomac.client.place.ImageListPlace;
import br.usp.icmc.dilvan.aimTemplateViewer.client.AIMTemplateParser;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;


public class Web_pad implements EntryPoint {

	/**
	 * Logger for the app.
	 */
	private static final Logger log = Logger.getLogger(Web_pad.class.getName());
	
//	public static final Web_padServiceAsync rpcService= GWT.create(Web_padService.class);
	private final Place defaultPlace = new ImageListPlace("imagesList");
	
	 @Override
	public void onModuleLoad() {
		/**
		 * Catch and log uncaught exceptions
		 * In some debug situations it is better NOT to catch them
		 */
		GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
			@Override
			public void onUncaughtException(Throwable e) {
				log.log(Level.SEVERE, e.getMessage(), e);
			}
		});
		
		// Create ClientFactory using deferred binding so we can replace with different
		// impls in gwt.xml
		ClientFactory clientFactory = GWT.create(ClientFactory.class);
		EventBus eventBus = clientFactory.getEventBus();
		PlaceController placeController = clientFactory.getPlaceController();
		
		//	Init parser as soon as possible, as it goes to the server to get the
		//  templates and they have to be in the client the first time the annotation panel
		//  pops up.
		clientFactory.getRpcService().getAIMContainers(new AsyncCallback<HashMap<String, String>>(){

			@Override
			public void onFailure(Throwable caught) {}

			@Override
			public void onSuccess(HashMap<String, String> result) {
				AIMTemplateParser.setAIMTemplateContainers(result);
			}});
		
//		br.usp.icmc.biomac.client.aimTemplate.Parser.test(GlobalResources.INSTANCE.vASARITemplatesV1rv233MixedModalities().getText());		


		// Start ActivityManager for the main widget with our ActivityMapper
		ActivityMapper activityMapper = new AppActivityMapper(clientFactory);
		ActivityManager activityManager = new ActivityManager(activityMapper, eventBus);
		activityManager.setDisplay(clientFactory.getRootWidget());

		// Start PlaceHistoryHandler with our PlaceHistoryMapper
		AppPlaceHistoryMapper historyMapper= GWT.create(AppPlaceHistoryMapper.class);
		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
		historyHandler.register(placeController, eventBus, defaultPlace);

		// Goes to place represented on URL or default place
		historyHandler.handleCurrentHistory();
	}
}
